home *** CD-ROM | disk | FTP | other *** search
/ Aminet 25 / Aminet 25 (1998)(GTI - Schatztruhe)[!][Jun 1998].iso / Aminet / util / shell / ShellScr.lha / ShellScr / src / args.e next >
Encoding:
Text File  |  1998-04-10  |  4.9 KB  |  164 lines

  1. -> handle args from both WB and CLI using only a ReadArgs() template
  2. -> that is, allow parse tooltypes with shell template
  3. -> Used in ShellScr by Kyzer/CSG
  4.  
  5. ->example:
  6. ->  IF rdargs := readargs('CX_PRIORITY/N, CX_POPKEY, CX_POPUP/S',
  7. ->                       args:=NEW [0,0,0], wbmessage)
  8. ->
  9. ->    cx_pri := IF args[0] THEN Long(args[0]) ELSE 0
  10. ->    hotkey := IF args[1] THEN args[1] ELSE 'ctrl alt del'
  11. ->    popup  := args[2]
  12. ->    [....]
  13. ->    FreeArgs(rdargs)
  14. ->  ENDIF
  15.  
  16. -> FIXES:
  17. -> - (06.04.98) Gags when directories are selected. Fixed.
  18.  
  19. -> TODO:
  20. -> - support =YES and =NO tooltypes in switches
  21. -> - special case (list of icon names) to /M switched variables
  22.  
  23. OPT MODULE
  24.  
  25. MODULE 'icon', 'dos/rdargs', 'workbench/startup', 'workbench/workbench'
  26.  
  27. -> Basically, we run through all the selected icons' tooltypes from
  28. -> first (our own icon) to last. if the left part of a tooltype
  29. -> (in 'BLA=FISH', the left part is 'BLA') is judged by DOS to be
  30. -> a keyword from the template (this includes parsing aliases for
  31. -> keywords) then we insert it and its right part (if there is one)
  32. -> into a list in the appropriate place, which can overwrite tooltypes
  33. -> already there (eg, selected icons override default settings in our
  34. -> own icon). Once we have this list of keywords and values, we can make
  35. -> a single string with all these keywords and values, and give it to
  36. -> ReadArgs.
  37.  
  38. -> The code that splits a tooltype into left and right parts is clever
  39. -> enough to ignore 'disabled' tooltypes which begin with '(', and also
  40. -> ignores tooltypes beginning IM1= or IM2= for performance reasons (NewIcons
  41. -> contain a lot of these tooltypes for their icon data)
  42.  
  43. EXPORT PROC readargs(template:PTR TO CHAR, args, wbmsg:PTR TO wbstartup) HANDLE
  44.   DEF rdargs=NIL, tooltype, name, val, m, n, arg, len, dir,
  45.       arglst=NIL:PTR TO LONG,     -> a list to all our args and their values
  46.       dobj=NIL:PTR TO diskobject, -> diskobject of 'current icon'
  47.       tooltypes:PTR TO LONG       -> tooltypes of 'current icon'
  48.  
  49.   -> If we were run from shell, then just parse as normal
  50.   IF wbmsg=NIL THEN RETURN ReadArgs(template, args, NIL)
  51.  
  52.   -> make sure icon.library is available
  53.   IF (iconbase := OpenLibrary('icon.library', 36))=NIL THEN Raise("LIB")
  54.  
  55.   -> make a big enough list to hold all name/value pairs for template
  56.   len:=1;  n := template; WHILE m := n[]++ DO IF m="," THEN INC len
  57.   IF (arglst := List(len*2))=NIL THEN Raise("MEM")
  58.   SetList(arglst, ListMax(arglst))
  59.  
  60.   -> Go through all tooltypes in all selected icons
  61.   FOR m := 0 TO wbmsg.numargs-1
  62.     dir := CurrentDir(wbmsg.arglist[m].lock)
  63.     dobj := GetDiskObject(wbmsg.arglist[m].name)
  64.     CurrentDir(dir)
  65.  
  66.     IF dobj
  67.       IF tooltypes := dobj.tooltypes
  68.         WHILE tooltype := tooltypes[]++
  69.           -> split up tooltype
  70.           name, val := ttsplit(tooltype)
  71.           -> if in template, place into appropriate list entry
  72.           IF (arg:=FindArg(template, name))<>-1
  73.             n := arg*2
  74.             IF arglst[ n ] THEN DisposeLink(arglst[ n ])
  75.             IF arglst[n+1] THEN DisposeLink(arglst[n+1])
  76.             arglst[ n ] := name
  77.             arglst[n+1] := val
  78.           ELSE
  79.             DisposeLink(name)
  80.             DisposeLink(val)
  81.           ENDIF
  82.         ENDWHILE
  83.       ENDIF
  84.       FreeDiskObject(dobj); dobj:=NIL
  85.     ENDIF
  86.   ENDFOR
  87.  
  88.   -> calculate length of final 'arg string' to be parsed by ReadArgs()
  89.   len := 0
  90.   FOR n := 0 TO ListLen(arglst)-1
  91.     IF arg:=arglst[n] THEN len := len + 3 + EstrLen(arg)
  92.   ENDFOR
  93.  
  94.   IF (m := String(len))=NIL THEN Raise("MEM")
  95.   StrCopy(m, '')
  96.  
  97.   -> concatenate final arg settings into one big string
  98.   FOR n:=0 TO ListLen(arglst)-1
  99.     IF arg := arglst[n]
  100.       -> append either 'arg ' or '"arg" ' if arg has spaces in it
  101.       IF InStr(arg, ' ')=-1
  102.         StrAdd(m, arg)
  103.         StrAdd(m, ' ')
  104.       ELSE        
  105.         StrAdd(m, '"')
  106.         StrAdd(m, arg)
  107.         StrAdd(m, '" ')
  108.       ENDIF
  109.     ENDIF
  110.   ENDFOR
  111.  
  112.   -> perform the ReadArgs call on our constructed string
  113.   rdargs := ReadArgs(template, args,
  114.     [m, EstrLen(m), 0, NIL, NIL, 0, NIL, RDAF_NOPROMPT]:LONG
  115.   )
  116.  
  117.   -> throw away big string
  118.   DisposeLink(m)
  119.  
  120. EXCEPT DO
  121.   IF arglst
  122.     FOR n := 0 TO ListLen(arglst) DO IF arg:=arglst[n] THEN DisposeLink(arg)
  123.     DisposeLink(arglst)
  124.   ENDIF
  125.  
  126.   IF dobj     THEN FreeDiskObject(dobj)
  127.   IF iconbase THEN CloseLibrary(iconbase)
  128.  
  129.   ReThrow()
  130. ENDPROC rdargs
  131.  
  132. PROC ttsplit(s:PTR TO CHAR)
  133.   -> of something 'blah=foo', returns 'blah','foo'
  134.   -> of something 'blah', returns 'blah',NIL
  135.   -> of something beginning '(', 'IM1=' or 'IM2=', returns NIL,NIL
  136.  
  137.   DEF div, len, l, r
  138.  
  139.   IF s=NIL THEN RETURN NIL, NIL
  140.   IF (s[]="(") OR StrCmp(s, 'IM1=', 4) OR StrCmp(s, 'IM2=', 4) THEN
  141.     RETURN NIL, NIL
  142.  
  143.   len := StrLen(s)
  144.   div := InStr(s, '=')
  145.  
  146.   IF div=-1
  147.     l := String(len)
  148.     IF l THEN StrCopy(l, s)
  149.     RETURN l, NIL
  150.   ENDIF
  151.  
  152.   l := String(div+1)
  153.   r := String(len-div)
  154.  
  155.   IF (l AND r)=NIL
  156.     IF l THEN DisposeLink(l)
  157.     IF r THEN DisposeLink(r)
  158.     RETURN NIL, NIL
  159.   ENDIF
  160.  
  161.   MidStr(l, s, 0, div) 
  162.   MidStr(r, s, div+1) 
  163. ENDPROC l, r
  164.